//--------------------------------------------------------------//
// Render To Texture
//--------------------------------------------------------------//
//--------------------------------------------------------------//
// Pass 0
//--------------------------------------------------------------//

float3 LightPosition;
float3 LightTarget;
float4x4 matProjection;
float4x4 world;
float fNearClipPlane;
float fFarClipPlane;
float4x4 lightView;


struct VS_INPUT 
{
   float4 Position : POSITION0;
};

struct VS_OUTPUT 
{
   float4 Position    : POSITION0;
   float3 Pos         : TEXCOORD0;
};

VS_OUTPUT VS_MAIN0( VS_INPUT Input )
{
   VS_OUTPUT Output = (VS_OUTPUT)0;

   //float3 zAxis = normalize(LightTarget - LightPosition);
   //float3 lightUp = normalize(cross(zAxis, cross(float3(0.0, 1.0, 0.0), zAxis)));
   //float3 xAxis = normalize(cross(lightUp, zAxis));
   //float3 yAxis = normalize(cross(zAxis, xAxis));
   
   float4 position = mul( Input.Position, world );
   position = mul(position, lightView);
   //position -= float4(LightPosition.x, LightPosition.y, LightPosition.z, 0.0);
   //
   //position.x = dot(position, xAxis);
   //position.y = dot(position, yAxis);  
   //position.z = dot(position, zAxis);
   
   Output.Position = mul(position, matProjection);
   Output.Pos = Output.Position;
   
   return( Output );   
}


float4 PS_MAIN0(VS_OUTPUT input) : COLOR0
{   
   float depth = 1.0 - (input.Pos.z / ((fFarClipPlane) - fNearClipPlane));
   return( float4( depth, depth, depth, 1.0f ) );
}




//--------------------------------------------------------------//
// Pass 1
//--------------------------------------------------------------//

float4x4 matWorldViewProjection;
float4 ambient;
texture renderTexture_Tex;
sampler renderTexture = sampler_state
{
   Texture = <renderTexture_Tex>;
};

struct VS_OUTPUT2
{
   float4 Pos: POSITION;
   float2 Tex: TEXCOORD0;
};

VS_OUTPUT2 VS_MAIN1( float4 Pos: POSITION,
                float2 Tex:  TEXCOORD0 )
{
   VS_OUTPUT2 Out = (VS_OUTPUT2) 0; 
   Out.Pos = mul( Pos, matWorldViewProjection );
   Out.Tex = Tex;
   return Out;
}

float4 PS_MAIN1( float4 Diff: COLOR0,
             float2 Tex: TEXCOORD0 ) : COLOR
{
    return tex2D( renderTexture, Tex ) + ambient;
}


//--------------------------------------------------------------//
// Technique Section for Effect Workspace.Effect Group 1.Render To Texture
//--------------------------------------------------------------//
technique Render_To_Texture
{
   pass P0
   {
      VertexShader = compile vs_2_0 VS_MAIN0();
      PixelShader = compile ps_2_0 PS_MAIN0();
   }

   pass P1
   {
      VertexShader = compile vs_1_1 VS_MAIN1();
      PixelShader = compile ps_2_0 PS_MAIN1();
   }
}

